//
//  LunBoView.swift
//  StudyDY
//
//  Created by yinchangming on 2018/7/6.
//  Copyright © 2018年 yinchangming. All rights reserved.
//

import UIKit
let CEELID="LUNBOID"
class LunBoView: UIView {
    
    @IBOutlet weak var luboCollection: UICollectionView!
    @IBOutlet weak var pageView: UIPageControl!
    var lunBoTimer:Timer?
    var lunbos:[LunBoBean]?{
        didSet{
            
            pageView.numberOfPages=lunbos?.count ?? 0
            
            luboCollection.reloadData()
            luboCollection.delegate=self
            //假象无限轮播 往前滑动
            let indexPath=IndexPath(item: (lunbos?.count)! * 100, section: 0)
            luboCollection.scrollToItem(at: indexPath, at: UICollectionViewScrollPosition.centeredHorizontally, animated: false)
            //开启轮播
            startLunBo()
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        //设置控件不跟随父控件 拉伸而拉伸
        autoresizingMask = []
        luboCollection.bounces=false//第一页 与最后一页 是否有弹簧效果
        luboCollection.register(UINib(nibName: "LunBoCollectionCell", bundle: nil), forCellWithReuseIdentifier: CEELID)
        
    }
    override func layoutSubviews() {
        let layout =  luboCollection.collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize =  luboCollection.bounds.size// CGSize(width: kScreenW+20, height: kScreenW * 3 / 8)//
    }
}


extension LunBoView{
    class func  creatView() -> LunBoView{
        return  Bundle.main.loadNibNamed("LunBoView", owner: nil, options: nil)?.first as! LunBoView
    }
}
extension LunBoView :UICollectionViewDataSource,UICollectionViewDelegate{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return ( lunbos?.count ?? 0 ) * 1000 //乘以1000的目的是为了做轮播  假象无限 实际上只有count的1000倍
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let lunbo=lunbos![indexPath.item % (lunbos?.count)!]
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CEELID, for: indexPath) as! LunBoCollectionCell
        cell.lunBoItme=lunbo
        return cell
    }
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let offsetX = scrollView.contentOffset.x + kScreenW * 0.5
        //求偏移页数
        pageView.currentPage =  Int( offsetX / kScreenW) % (lunbos?.count)!
    }
    //开始滑动 停止轮播
    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        stopLunBo()
    }
    //手动 滚动完毕 开启轮播
    func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        startLunBo()
    }
}
extension LunBoView{
    //开启轮播  timeInterval 时间间隔  target 监听对象  selector 回调的方法  repeats 是否重复
    func startLunBo(){
        lunBoTimer=Timer(timeInterval: 3, target: self, selector: #selector(doSomething), userInfo: nil, repeats: true)
        RunLoop.main.add(lunBoTimer!, forMode: RunLoopMode.commonModes)
    }
    func stopLunBo(){
        lunBoTimer?.invalidate()
        lunBoTimer=nil
    }
    @objc func doSomething(){
        //获取当前偏移量
        let currentX=luboCollection.contentOffset.x
        let mubiaoX=currentX+kScreenW
        luboCollection.setContentOffset(CGPoint(x: mubiaoX, y: 0), animated: true)
    }
}

